package com.wyl.mybatis.config;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

/**
 * @Description
 * @Author WuYiLong
 * @Date 2024/3/14 9:43
 */
public class SqlSessionFactoryConfig {

    /**
     * 默认配置
     * @return
     * @throws IOException
     */
    public static SqlSessionFactory buildSqlSessionFactory() throws IOException {
        // 读取mybatis的配置文件
        InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");
        // 通过SqlSessionFactoryBuilder 的 build 方法用于解析配置文件并创建 SqlSessionFactory 对象。
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        return sqlSessionFactory;
    }

    /**
     * 多环境配置
     * @param environment
     * @return
     * @throws IOException
     */
    public static SqlSessionFactory buildSqlSessionFactoryEnv(String environment) throws IOException {
        // 读取mybatis的配置文件
        InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");
        // 通过SqlSessionFactoryBuilder 的 build 方法用于解析配置文件并创建 SqlSessionFactory 对象。
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream,environment);
        return sqlSessionFactory;
    }
}
